package net.aiform.template.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.aiform.account.pojo.Person;
import net.aiform.account.service.PersonService;
import net.aiform.bean.JSONMessage;
import net.aiform.template.pojo.Template;
import net.aiform.template.service.TemplateService;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;

public class ListTemplateAction implements Preparable, ServletRequestAware {
	private Logger log = Logger.getLogger(this.getClass());
	private JSONMessage json;
	private PersonService personService;
	private TemplateService templateService;
	private HttpServletRequest req;
	private Person person;
	private Template template;
	private String arg1;

	public String listTemplates() {
		String code = this.req.getServletPath();
		code = code.substring(code.lastIndexOf("/") + 1);
		List<Template> ts = new ArrayList<Template>();
		if ("all".equalsIgnoreCase(code)) {
			ts = this.templateService.findAll();
		} else {
			ts.add(this.templateService.get(code));
		}
		json.setData(ts);
		return Action.SUCCESS;
	}

	public String execute() {
		return Action.SUCCESS;
	}

	@JSON(serialize = false)
	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public ListTemplateAction() {
		super();
		this.json = new JSONMessage();
	}

	public ListTemplateAction(PersonService personService, TemplateService templateService) {
		this.personService = personService;
		this.templateService = templateService;
		this.json = new JSONMessage();
	}

	public void prepare() throws Exception {

	}

	@JSON(serialize = false)
	public String getArg1() {
		return arg1;
	}

	public void setArg1(String arg1) {
		this.arg1 = arg1;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.req = request;
	}

	@JSON(serialize = false)
	public TemplateService getTemplateService() {
		return templateService;
	}

	public void setTemplateService(TemplateService templateService) {
		this.templateService = templateService;
	}

	@JSON(serialize = false)
	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	@JSON(serialize = false)
	public Template getTemplate() {
		return template;
	}

	public void setTemplate(Template template) {
		this.template = template;
	}

	@JSON(serialize = false)
	public Logger getLog() {
		return log;
	}

	public void setLog(Logger log) {
		this.log = log;
	}

	public void setReq(HttpServletRequest req) {
		this.req = req;
	}

	public JSONMessage getJson() {
		return json;
	}

	public void setJson(JSONMessage json) {
		this.json = json;
	}

	@JSON(serialize = false)
	public HttpServletRequest getReq() {
		return req;
	}
}
